<?php

/**
 * Description of Helpers_ProductTable
 *
 * @author a.novikov
 */
class Helpers_ProductTableHasbrogame extends Helpers_Base_Paginator {

	const IS_PRESENT = 'is_present';
	const ORDER = 'order';
	const ORDER_DIRECTION = 'order_direction';
	const VIEW_PATH = 'layouts/blocks/product_hasbrogame_table.tpl';


	/**
	 *
	 * @return array
	 */
	protected function getPageParams() {

		$isPresent = (bool)$this->_getParam('is_present', false);
		if (isset($_COOKIE[self::IS_PRESENT])) {
			$isPresent = (boolean)$_COOKIE[self::IS_PRESENT];
		}

		$orderTypes = array(
			'rank' => 'Популярности',
			'price'=>'Цене',
			'name'=>'Алфавиту',
			'rating'=>'Рейтингу'
		);
		
		$order = $this->_getParam('order', 'rank');

		if (isset($_COOKIE[self::ORDER])) {
			$order = $_COOKIE[self::ORDER];
		}

		if ( !isset($orderTypes[$order]) ) {
			$order = 'name';
		}
		
		$orderDirection = $this->_getParam('order_direction', 'desc');
		
		if (isset($_COOKIE[self::ORDER_DIRECTION])) {
			$orderDirection = $_COOKIE[self::ORDER_DIRECTION];
		}


		if ($orderDirection != 'asc') {
			$orderDirection = 'desc';
		}

		return array(
			'paginatorParams' => $this->getPaginatorParams(),
			'isPresent'	      => $isPresent,
			'order'			  => array(
								'field'     => $order,
								'direction'	=> $orderDirection
			),
			'orderTypes' => $orderTypes
		);
	}

	public function ProductTableHasbrogame(Db_Select $productSelect, $product_select_temp = null) {
		extract( $this->getPageParams() );

		$productSelect_family = clone($productSelect);
		$productSelect_kids1 = clone($productSelect);
		$productSelect_kids2 = clone($productSelect);
		$productSelect_road = clone($productSelect);
		$productSelect_card = clone($productSelect);

		$paginatorParams_family['per_page'] = 4;
		$paginatorParams_kids1['per_page'] = 4;
		$paginatorParams_kids2['per_page'] = 4;
		$paginatorParams_road['per_page'] = 4;
		$paginatorParams_card['per_page'] = 4;
        
        $paginatorParams_family['range_page'] = 6;
		$paginatorParams_kids1['range_page'] = 6;
		$paginatorParams_kids2['range_page'] = 6;
		$paginatorParams_road['range_page'] = 6;
		$paginatorParams_card['range_page'] = 6;

		$paginatorParams_family['page'] = 1;
		$paginatorParams_kids1['page'] = 1;
		$paginatorParams_kids2['page'] = 1;
		$paginatorParams_road['page'] = 1;
		$paginatorParams_card['page'] = 1;

		$p_id = $this->_getParam('p_id' , 0);
		$page = $this->_getParam('page' , 0);
		switch ($p_id) {
			case "family":
				$paginatorParams_family['page'] = $page;
				break;
			case "kids1":
				$paginatorParams_kids1['page'] = $page;
				break;
			case "kids2":
				$paginatorParams_kids2['page'] = $page;
				break;
			case "road":
				$paginatorParams_road['page'] = $page;
				break;
			case "card":
				$paginatorParams_card['page'] = $page;
				break;
		}

		//@todo рассмотреть возможность мягкой замены на флаг is_present
		/* есть даже загвоздка, кажется ни кто не учитывал что может возникнуть 
		 * ситуация когда подтовар есть а товара-карточки нет, в этом случае данный флаг бесполезен
		 */

		if (isset($_COOKIE['per_page'])) {
			$per_page = (int)$_COOKIE['per_page'];
		}

		$this->view->order = $order['field'];
		$this->view->order_direction =  $order['direction'];
		
		$this->view->is_present = $isPresent;
		$this->view->order_types = $orderTypes;

		$productPeer = ProductPeer::getInstance();

		$productSelect_family->setIntegrityCheck(false);
		$productSelect_family->join('product_collection', 'product_collection.id = product.collection_id', array('col_key' => 'key'));
		$productSelect_family->where(ProductPeer::IS_ACTIVE, 1);
		$productSelect_family->order($order['field'].' '.$order['direction']);
		$productSelect_family->order('id desc');

		$productSelect_kids1->setIntegrityCheck(false);
		$productSelect_kids1->join('product_collection', 'product_collection.id = product.collection_id', array('col_key' => 'key'));
		$productSelect_kids1->where(ProductPeer::IS_ACTIVE, 1);
		$productSelect_kids1->order($order['field'].' '.$order['direction']);
		$productSelect_kids1->order('id desc');

		$productSelect_kids2->setIntegrityCheck(false);
		$productSelect_kids2->join('product_collection', 'product_collection.id = product.collection_id', array('col_key' => 'key'));
		$productSelect_kids2->where(ProductPeer::IS_ACTIVE, 1);
		$productSelect_kids2->order($order['field'].' '.$order['direction']);
		$productSelect_kids2->order('id desc');

		$productSelect_road->setIntegrityCheck(false);
		$productSelect_road->join('product_collection', 'product_collection.id = product.collection_id', array('col_key' => 'key'));
		$productSelect_road->where(ProductPeer::IS_ACTIVE, 1);
		$productSelect_road->order($order['field'].' '.$order['direction']);
		$productSelect_road->order('id desc');

		$productSelect_card->setIntegrityCheck(false);
		$productSelect_card->join('product_collection', 'product_collection.id = product.collection_id', array('col_key' => 'key'));
		$productSelect_card->where(ProductPeer::IS_ACTIVE, 1);
		$productSelect_card->order($order['field'].' '.$order['direction']);
		$productSelect_card->order('id desc');

		$productSelect_family->where("product_collection.key = 'family_game'");
		$productSelect_kids1->where("product_collection.key = 'kids_game1'");
		$productSelect_kids2->where("product_collection.key = 'kids_game2'");
		$productSelect_road->where("product_collection.key = 'road_game'");
		$productSelect_card->where("product_collection.key = 'card_game'");

		$paginator_family = ProductService::getInstance()->getPaginatorBySelect(
			$productPeer, $productSelect_family, $paginatorParams_family );
		$paginator_kids1 = ProductService::getInstance()->getPaginatorBySelect(
			$productPeer, $productSelect_kids1, $paginatorParams_kids1 );
		$paginator_kids2 = ProductService::getInstance()->getPaginatorBySelect(
			$productPeer, $productSelect_kids2, $paginatorParams_kids2 );
		$paginator_road = ProductService::getInstance()->getPaginatorBySelect(
			$productPeer, $productSelect_road, $paginatorParams_road );
		$paginator_card = ProductService::getInstance()->getPaginatorBySelect(
			$productPeer, $productSelect_card, $paginatorParams_card );

		$this->view->products_paginator_family = $paginator_family;
		$this->view->products_paginator_kids1 = $paginator_kids1;
		$this->view->products_paginator_kids2 = $paginator_kids2;
		$this->view->products_paginator_road = $paginator_road;
		$this->view->products_paginator_card = $paginator_card;

		$this->view->products_family = ContentList::factoryByClassName('List_Product_CategoryTemp', $paginator_family);
		$this->view->products_kids1 = ContentList::factoryByClassName('List_Product_CategoryTemp', $paginator_kids1);
		$this->view->products_kids2 = ContentList::factoryByClassName('List_Product_CategoryTemp', $paginator_kids2);
		$this->view->products_road = ContentList::factoryByClassName('List_Product_CategoryTemp', $paginator_road);
		$this->view->products_card = ContentList::factoryByClassName('List_Product_CategoryTemp', $paginator_card);

		return $this->render();
	}

	public function getViewPath(){
		return self::VIEW_PATH;
	}
}